package SpringMVC.web;

import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Random;

@Controller
@RequestMapping("/login")
public class KSController {
    OkHttpClient client = new OkHttpClient();
    public final static String ERROR = "error: 1,message : 登录失败，请确认是否输入正确的账号和密码";
    public final static String SUCCESS = "success";

    @RequestMapping("/doLogin")
    @ResponseBody
    public String login(HttpServletRequest request, @RequestParam(name = "uid", defaultValue = "18682635069") String name, @RequestParam(name = "pwd", defaultValue = "huayang520") String password, String code) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        RequestBody params = new FormBody.Builder()
                .add("timestamp", timestamp)
                .add("uid", name)
                .add("pwd", password)
                .build();
//        Request request1 = new Request.Builder()
//                .url("https://coyee.com/signin")
//                .post(params)
//                .build();
//        Response codeResponse = null;
//        try {
//            codeResponse = client.newCall(request1).execute();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if (codeResponse.isSuccessful()) {
//
//            List<String> cookieStr = codeResponse.headers("Set-Cookie");
//            String cookie = "";
//            for (String one : cookieStr) {
//                cookie += one.substring(0, one.indexOf(';')) + ";";
//            }
//            cookie = cookie.substring(0, cookie.length()-1);
//            request.getSession(true).setAttribute("cookie", cookie);
//        }
        String cookie = "";
        Request login = new Request.Builder()
                .url("https://coyee.com/action/user/signin")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Referer", "https://coyee.com/signin")
                .addHeader("Cookie", cookie)
                .post(params)
                .build();
        Response result = null;
        try {
            result = client.newCall(login).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (result.isSuccessful()) {
            try {
                cookie = String.valueOf(result.headers("Set-Cookie"));
                request.getSession().setAttribute("Cookie", cookie);
                System.out.println(cookie);
                System.out.println(result.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSON.toJSONString(SUCCESS);
    }

    @RequestMapping("/add")
    @ResponseBody
    public String add(HttpServletRequest request, @RequestParam(name = "user", defaultValue = "12048") String name, String memo, @RequestParam(name = "userCode", defaultValue = "AGSFuiA0uySWKiRmeafnAnfji8hxOXXXfa7cZudZ") String userCode) {
        String timestamp = String.valueOf(System.currentTimeMillis());
        String[] s = new String[]{":bowtie:", ":smile:", ":laughing:", ":blush:", ":smiley:",
                ":relaxed:", ":smirk:", ":heart_eyes:", ":kissing_heart:",
                ":kissing_closed_eyes:", ":flushed:", ":relieved:", ":satisfied:",
                ":grin:", ":wink:", ":stuck_out_tongue_winking_eye:", ":stuck_out_tongue_closed_eyes:",
                ":grinning:", ":kissing:", ":kissing_smiling_eyes:", ":stuck_out_tongue:", ":sleeping:",
                ":worried:", ":frowning:", ":anguished:", ":open_mouth:", ":grimacing:", ":confused:",
                ":hushed:", ":expressionless:", ":unamused:", ":sweat_smile:", ":sweat:", ":weary:",
                ":pensive:", ":disappointed:", ":confounded:", ":fearful:", ":cold_sweat:", ":persevere:",
                ":cry:", ":sob:", ":joy:", ":astonished:", ":scream:", ":tired_face:", ":angry:", ":rage:",
                ":triumph:", ":sleepy:", ":yum:", ":mask:", ":sunglasses:", ":dizzy_face:", ":imp:",
                ":smiling_imp:", ":neutral_face:", ":innocent:", ":alien:", ":heart:", ":cupid:",
                ":sparkles:", ":boom:", ":exclamation:", ":question:", ":zzz:", ":thumbsup:", ":-1:",
                ":ok_hand:", ":punch:", ":fist:", ":v:", ":open_hands:", ":point_up:", ":point_down:",
                ":point_left:", ":point_right:", ":raised_hands:", ":pray:", ":clap:", ":muscle:",
                ":metal:", ":kiss:", ":trollface:"};
        Random random = new Random();
        int a = random.nextInt(s.length);
        RequestBody params = new FormBody.Builder()
                .add("timestamp", timestamp)
                .add("user", name)
                .add("memo", s[a])
                .add("userCode", userCode)
                .build();

        Request login = new Request.Builder()
                .url("https://coyee.com/action/tweet/add")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Referer", "https://coyee.com/zhi")
                .addHeader("Cookie", (String) request.getSession().getAttribute("Cookie"))
                .post(params)
                .build();
        Response response = null;
        try {
            response = client.newCall(login).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (response.isSuccessful()) {
            try {
                System.out.println(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return JSON.toJSONString(SUCCESS);
    }
}


